package com.example.myblog.controller;

import com.example.myblog.model.ArticleInfo;
import com.example.myblog.model.UserInfo;
import com.example.myblog.service.UserService;
import com.example.myblog.util.ReturnData;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date:2022-10-28
 * Time:22:55
 *
 * @author 东莞呵呵
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/reg")
    public ReturnData reg(UserInfo userinfo) {
        if (userinfo != null && StringUtils.hasLength(userinfo.getUsername()) &&
                StringUtils.hasLength(userinfo.getPassword()) &&
                StringUtils.hasLength(userinfo.getNickname())) {
            int ret = userService.reg(userinfo);
            return ReturnData.ok("", ret);
        } else {
            return ReturnData.fail("非法参数", null);
        }
    }

    @RequestMapping("/login")
    public ReturnData login(UserInfo userInfo, HttpServletRequest request) {
        //设置session
        UserInfo retUserInfo = userService.login(userInfo);
        HttpSession session = request.getSession(true);
        session.setAttribute("userinfo", retUserInfo);

        if (retUserInfo == null) {
            return ReturnData.ok("该用户未注册", 0);
        } else if (!userInfo.getPassword().equals(retUserInfo.getPassword())) {
            return ReturnData.fail("用户名或密码错误", 0);
        } else {
            return ReturnData.ok("", userInfo);
        }
    }

    @RequestMapping("/artlist")
    public ReturnData index(Integer cp, Integer ps) {
        Integer currPage = cp;
        Integer pageSize = ps;
        if (currPage == null || pageSize == null) {
            return ReturnData.ok("参数非法", null);
        } else {
            Integer offsetSize = (currPage - 1) * pageSize;
            List<ArticleInfo> articleInfos = userService.index(offsetSize, pageSize);
            return ReturnData.ok("", articleInfos);
        }

    }

    @RequestMapping("/artdetail")
    public ReturnData getArtByAid(ArticleInfo articleInfo) {
        if (articleInfo.getAid() <= 0) {
            return ReturnData.fail("参数非法", null);
        } else {
            ArticleInfo retArticle = userService.getArtByAid(articleInfo);
            return ReturnData.ok("", retArticle);
        }
    }

    @RequestMapping("/getuserbyaid")
    public ReturnData getUserByAid(ArticleInfo articleInfo) {
        if (articleInfo.getAid() <= 0) {
            return ReturnData.fail("参数非法", null);
        } else {
            UserInfo userInfo = userService.getUserByAid(articleInfo);
            return ReturnData.ok("", userInfo);
        }
    }

    @RequestMapping("/addrcount")
    public ReturnData readCountIncrease(ArticleInfo articleInfo) {
        if (articleInfo.getAid() <= 0) {
            return ReturnData.fail("参数非法", null);
        } else {
            int ret = userService.readCountIncrease(articleInfo);
            return ReturnData.ok("", articleInfo.getReadcount());
        }
    }

    @RequestMapping("/loginUserInfo")
    public ReturnData isLogin(@SessionAttribute(value = "userinfo", required = false) UserInfo userinfo) {
        return ReturnData.ok("", userinfo);
    }

    @RequestMapping("/logout")
    public ReturnData logout(HttpServletRequest request) {
        HttpSession session=request.getSession();
        if (session!=null) {
            session.removeAttribute("userinfo");
            return ReturnData.ok("", 1);
        } else {
            return ReturnData.fail("未登录", null);
        }
    }

    @RequestMapping("/myartlist")
    public ReturnData createArtList(UserInfo userInfo) {
        if (userInfo == null || userInfo.getUid() <= 0) {
            return ReturnData.fail("参数非法", null);
        } else {
            List<ArticleInfo> list = userService.createArtList(userInfo);
            return ReturnData.ok("", list);
        }
    }

    @RequestMapping("/delart")
    public ReturnData delArticle(ArticleInfo articleInfo) {
        if (articleInfo == null || articleInfo.getAid() <= 0) {
            return ReturnData.fail("参数非法", null);
        } else {
            int ret = userService.delArtByAid(articleInfo);
            return ReturnData.fail("", ret);
        }
    }

    @RequestMapping("/addart")
    public ReturnData addArticle(ArticleInfo articleInfo, @SessionAttribute(value = "userinfo", required = false) UserInfo userInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle()) || !StringUtils.hasLength(articleInfo.getContent())){
            return ReturnData.fail("参数非法",null);
        }
        int length=0;
        if(((String)articleInfo.getContent()).length()<100){
            length=((String)articleInfo.getContent()).length();
        }else {
            length=100;
        }
        articleInfo.setDepict((String)articleInfo.getContent().subSequence(0,length));
        articleInfo.setUid(userInfo.getUid());
        int ret=userService.addArt(articleInfo);
        return ReturnData.ok("",ret);
    }
}
